/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package bllImplement;

import bllInterface.Account;
import bllInterface.BLLRelation;
import bllobject.AccountImpl;
import dalfactory.SDALFactory;
import java.rmi.RemoteException;
import java.util.ArrayList;
import utils.DataRow;
import utils.DataTable;

/**
 *
 * @author Mrkupi
 */
public class BLLRelationImpl implements BLLRelation
{
    private static BLLRelationImpl inst;
    
    private BLLRelationImpl()
    {
    }

    public static BLLRelation getInstance()
    {
        if( inst    ==  null )
        {
            inst    =   new BLLRelationImpl();
        }
        return inst;
    }
    
    @Override
    public ArrayList<Account> getFriendList( String userName ) throws RemoteException 
    {
        ArrayList<Account> accs =   new ArrayList();
        DataTable dtRelation    =   SDALFactory.getInstance().getRelation().getFriendOf( userName );
        if( dtRelation != null )
        {
            for( int i = 0; i < dtRelation.size(); ++i )
            {
                DataRow row         =   dtRelation.get( i );
                String frName       =   row.get( "USERNAME1" ).equals(userName) ? row.get( "USERNAME2" ) : row.get( "USERNAME1" );
                DataTable dtAccount =   SDALFactory.getInstance().getAccount().getAccountInfo( frName );
                if( dtAccount != null )
                {
                    DataRow rowFr   =   dtAccount.get( 0 );
                    accs.add( AccountImpl.createRemoteAccount(frName, rowFr.get("UserName"), Integer.parseInt(rowFr.get( "Experience" )), Integer.parseInt(rowFr.get( "Level" )), "0"));
                }
            }
        }
        return accs;
    }
    
    @Override
    public ArrayList<String> getRequestedFriend(String userName) throws RemoteException 
    {
        DataTable dt = SDALFactory.getInstance().getRelation().getRequestedFriend(userName);
        ArrayList<String> requests  =   new ArrayList();
        if ( dt != null && dt.size() > 0)
        {
            for( int i = 0; i < dt.size(); ++i )
            {
                DataRow row = dt.get(i);
                requests.add( row.get( "RequestFrom" ) );
            }
        }
        
        return requests;
    }

    @Override
    public boolean sendRequestFriend(String userName, String toFriend) throws RemoteException 
    {
        return SDALFactory.getInstance().getRelation().requestFriend(userName, toFriend);
    }

    @Override
    public boolean insertNewRelationShip(String userName, String friend) throws RemoteException 
    {
        return SDALFactory.getInstance().getRelation().insertNewRelationShip(userName, friend);
    }

    @Override
    public boolean deleteRequestFriend(String userName, String requester) throws RemoteException 
    {
        return SDALFactory.getInstance().getRelation().deleteRequest(userName, requester);
    }
}
